package com.chen.nongansampling;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.chen.nongansampling.adapter.ListViewItemAdapter;
import com.chen.nongansampling.adapter.ListviewEditTextAdapter;
import com.chen.nongansampling.adapter.ListviewTextAdapter;
import com.chen.nongansampling.adapter.PictureAddGridAdapter;
import com.chen.nongansampling.app.BaseApplication;
import com.chen.nongansampling.bean.AddOperatorBean;
import com.chen.nongansampling.bean.Callback;
import com.chen.nongansampling.bean.SimpleRequest;
import com.chen.nongansampling.bean.request.InsertOperatingRequest;
import com.chen.nongansampling.bean.sample.TypeList;
import com.chen.nongansampling.bean.tanchubean;
import com.chen.nongansampling.bean.ui.ListviewBean;
import com.chen.nongansampling.custom.CustomScrollGridView;
import com.chen.nongansampling.fragment.BusinessArchivesActivity;
import com.chen.nongansampling.model.CallbackData;
import com.chen.nongansampling.model.Market;
import com.chen.nongansampling.presenter.AddOperatorPresenter;
import com.chen.nongansampling.presenterView.AddOperatorView;
import com.chen.nongansampling.utils.CommandPhotoUtil;
import com.chen.nongansampling.utils.ImageUtil;
import com.chen.nongansampling.utils.PhotoBitmapUtil;
import com.chen.nongansampling.utils.PhotoSystemOrShoot;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AddOperatorFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AddOperatorFragment extends Fragment implements AddOperatorView {




    /**
     * 存放图片的容器
     */
    private CustomScrollGridView mGridView;

    /**
     * GridView适配器
     */
    private PictureAddGridAdapter gridAdapter;

    /**
     * 管理图片操作
     */
    private CommandPhotoUtil commandPhoto;

    /**
     * 选择图片来源
     */
    private PhotoSystemOrShoot selectPhoto;

    private AddOperatorPresenter samplePresenter=new AddOperatorPresenter(getContext());

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private CallbackData<List<TypeList>> typelists;
    @BindView(R.id.sample_text_listview)
    ListView sampleTextListview;
    @BindView(R.id.sample_text_listviewXiala)
    ListView sampleTextListviewXiala;
    @BindView(R.id.sample_text_xinyong)
    ListView sampleTextListviewXinyong;

    ListView sampleTextListviewkongbai;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.listview_text_name)
    TextView listviewTextName;
    @BindView(R.id.red_star)
    TextView red_star;

    public  static Context  context;
    ListviewEditTextAdapter listviewEditTextAdapter;
    ListviewEditTextAdapter listviewEditTextAdapter1;
    ListviewTextAdapter listviewEditTextAdapter2;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String shichang;

    private List<String> choose = new ArrayList<>();
    private List<String> chooseShow = new ArrayList<>();
    private List<String> nameid = new ArrayList<>();
    private List<String> nameid2 = new ArrayList<>();
    AddOperatorBean  operatingBean=new AddOperatorBean();;

    List<Market> marketList=new ArrayList<>();  //市场列表
    List<TypeList> typeList=new ArrayList<>();  //市场列表
    public AddOperatorFragment() {

    }
    public AddOperatorFragment(Context context) {
        this.context=context;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AddOperatorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AddOperatorFragment newInstance(String param1, String param2) {
        AddOperatorFragment fragment = new AddOperatorFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public static AddOperatorFragment newInstance() {
        return new AddOperatorFragment();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getWindow().setDimAmount(0);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_add_operator, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
    AddOperatorPresenter addOperatorPresenter=new AddOperatorPresenter(getContext());

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initListView();
        addOperatorPresenter.BindPresentView(this);

        //if(bianjiid==1){
        ImageButton imageButton=getActivity().findViewById(R.id.add_operator);
        imageButton.setVisibility(View.INVISIBLE);

        listviewTextName.setText("营业执照");
        red_star.setVisibility(View.GONE);
        //}else if(bianjiid==2){

        //Button imageButton=getActivity().findViewById(R.id.buttonbianji);
        //imageButton.setVisibility(View.INVISIBLE);
        //listviewTextName.setText("营业执照");
        //}
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println(selectPhoto);
        super.onActivityResult(requestCode, resultCode, data);
        if (selectPhoto != null) {

            String photoPath = selectPhoto.getPhotoResultPath(requestCode, resultCode, data);
            if (!TextUtils.isEmpty(photoPath)) {
                commandPhoto.showGridPhoto(photoPath);
            }
        }
    }



    public void initListView() {
        //    listView=getView().findViewById(R.id.sample_text_listview);
        //if(bianjiid==1){

        ListviewBean<String> list1 = new ListviewBean();
        list1.setData("");
        list1.setImageSrc(R.mipmap.username);
        list1.setName("姓名");
        list1.setRedStar(true);

        ListviewBean<String> list2 = new ListviewBean();
        list2.setData("");
        list2.setImageSrc(R.mipmap.tanweihao);
        list2.setName("摊位号");
        list2.setRedStar(true);

        ListviewBean<String> list3 = new ListviewBean();
        list3.setData("请选择");
        list3.setImageSrc(R.mipmap.shichangname);
        list3.setName("市场名称");
        list3.setRedStar(true);

        ListviewBean<String> list4 = new ListviewBean();
        list4.setData("");
        list4.setImageSrc(R.mipmap.phone);
        list4.setName("联系电话");


        ListviewBean<String> list5 = new ListviewBean();
        list5.setData("请选择");
        list5.setImageSrc(R.mipmap.jingyingtype);
        list5.setName("经营种类");
        list5.setRedStar(true);

        ListviewBean<String> list6 = new ListviewBean();
        list6.setData("");
        list6.setImageSrc(R.mipmap.listview_operator_social_number);
        list6.setName("统一社会信用代码");


//        ListviewBean<Bitmap> list6=new ListviewBean();
//        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
//        list6.setData(bitmap);
//        list6.setImageSrc(R.mipmap.listview_text_test_method);
//        list6.setName("采样照片");
//        list6.setComplicated(true);


        List<ListviewBean> list = new ArrayList<>();
        list.add(list1);
        list.add(list2);
        list.add(list4);
        List<ListviewBean> listE = new ArrayList<>();
        listE.add(list3);
        listE.add(list5);
        List<ListviewBean> listXinyong = new ArrayList<>();
        listXinyong.add(list6);
        listviewEditTextAdapter=new ListviewEditTextAdapter(list, getContext());
        sampleTextListview.setAdapter(listviewEditTextAdapter);
        sampleTextListviewXiala.setAdapter(new ListviewTextAdapter(listE,getContext(),false));
        listviewEditTextAdapter1=new ListviewEditTextAdapter(listXinyong, getContext());
        sampleTextListviewXinyong.setAdapter(listviewEditTextAdapter1);
        sampleTextListviewXiala.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:{


                        if (marketList.size()!=0) {
                            LayoutInflater inflater = LayoutInflater.from(getContext());
                            View layout = inflater.inflate(R.layout.edit_select, null);
                            final EditText edit = (EditText)layout.findViewById(R.id.listview_edit_data);
                            final TextView title = (TextView)layout.findViewById(R.id.listview_title_name);
                            edit.setHint("请输入菜市场名称");
                            title.setText("请选择市场");
                            String s[]=new String[marketList.size()];
                            List<tanchubean> list=new ArrayList<>();
                            for (int i=0;i<marketList.size();i++)
                            {
                                s[i]=marketList.get(i).getName();
                                tanchubean<String> list1=new tanchubean();
                                list1.setName(marketList.get(i).getName());
                                list.add(list1);
                            }
                            ListViewItemAdapter adapter = new ListViewItemAdapter(getContext(),list);
                            AlertDialog dialogs = new AlertDialog.Builder(getContext())
                                    .setCustomTitle(layout)
                                    //.setView(list)
                                    .setPositiveButton("搜索市场",null)
                                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();

                                            TextView t = view.findViewById(R.id.listview_text_data);
                                            t.setText(marketList.get(which).getName());
                                            t.setTextColor(Color.BLACK);
                                            operatingBean.setMarketId(marketList.get(which).getPkId());
                                            shichang=marketList.get(which).getName();

                                        }
                                    }).create();
                            dialogs.show();
                            // 通过Activity类中的getWindowManager()方法获取窗口管理，再调用getDefaultDisplay()方法获取获取Display对象
                            Display display = getActivity().getWindowManager().getDefaultDisplay();
                            // 方法一(推荐使用)使用Point来保存屏幕宽、高两个数据
                            Point outSize = new Point();
                            // 通过Display对象获取屏幕宽、高数据并保存到Point对象中
                            display.getSize(outSize);
                            // 从Point对象中获取宽、高
                            int x = (new Double(outSize.x*0.9)).intValue();
                            int y = (new Double(outSize.y*0.9)).intValue();

                            dialogs.getWindow().setLayout(x,y);
                            //弹出对话框时需要点击输入框才能弹出软键盘
                            dialogs.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                            dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(13);
                            Drawable dr = getActivity().getResources().getDrawable(R.drawable.login_btn_selector); //取得图片资源
                            dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setBackground(dr);
                            LinearLayout.LayoutParams PositiveButton =
                                    (LinearLayout.LayoutParams)  dialogs.getButton(DialogInterface.BUTTON_POSITIVE).getLayoutParams();
                            PositiveButton.height = (new Double(outSize.x*0.12)).intValue();
                            PositiveButton.width = (new Double(outSize.x*0.3)).intValue();
                            //设置文字居中
                            PositiveButton.gravity = Gravity.CENTER;
                            PositiveButton.setMargins(new Double(outSize.x-outSize.x*0.3).intValue()/2,
                                    20, new Double(outSize.x-outSize.x*0.3).intValue()/2, 20);
                            dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (!TextUtils.isEmpty(edit.getText().toString())) {
                                        ArrayList<String> strlist = new ArrayList<String>();
                                        ArrayList<Market> marketLists = new ArrayList<Market>();
                                        List<tanchubean> list=new ArrayList<>();
                                        for (int i = 0; i < marketList.size(); i++) {
                                            if(marketList.get(i).getName().indexOf(edit.getText().toString()) != -1){
                                                strlist.add(marketList.get(i).getName());
                                                marketLists.add(marketList.get(i));
                                                tanchubean<String> list1=new tanchubean();
                                                list1.setName(marketList.get(i).getName());
                                                list.add(list1);
                                            }
                                        }
                                        String s[] = (String[]) strlist.toArray(new String[strlist.size()]);
                                        ListViewItemAdapter adapter = new ListViewItemAdapter(getContext(),list);

                                        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext())
                                                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        dialog.dismiss();
                                                        dialogs.dismiss();
                                                        TextView t = view.findViewById(R.id.listview_text_data);
                                                        t.setText(marketLists.get(which).getName());
                                                        t.setTextColor(Color.BLACK);
                                                        operatingBean.setMarketId(marketLists.get(which).getPkId());
                                                        shichang=marketList.get(which).getName();
                                                    }
                                                });
                                        dialog.create().show();

                                    }

                                }
                            });
                        }

                    }break;
                    case 1:{
                        if (typelists.getData()!=null) {

                            String s[]=new String[typelists.getData().size()];
                            for (int i=0;i<typelists.getData().size();i++)
                            {
                                s[i]=typelists.getData().get(i).getTypeName();
                            }

                            choose.clear();
                            nameid.clear();
                            AlertDialog builder = new AlertDialog.Builder(getContext())
                                    .setTitle("请选择经营种类")
                                    .setIcon(R.mipmap.xuanze)

                                    .setMultiChoiceItems(s,null, new DialogInterface.OnMultiChoiceClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                            if(isChecked){
                                                // TextView t = view.findViewById(R.id.listview_text_data);
                                                //t.setTextColor(Color.BLACK);
                                                choose.add(typelists.getData().get(which).getTypeName());
                                                //t.setText(typeList.get(which).getName());
                                                //jingyingtype=typelists.getData().get(which).getTypeName();
                                                if (typelists.getData().get(which).getTypeId()!=null){
                                                    // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getName());
                                                    // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getPkId());
                                                    //operatingBean.setTypePkId(typeList.get(which).getPkId());
                                                    nameid.add(typelists.getData().get(which).getTypeId());
                                                }
                                            }else{
                                                choose.remove(typelists.getData().get(which).getTypeName());
                                                nameid.remove(typelists.getData().get(which).getTypeId());
                                            }

                                        }
                                    })
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if(choose.size()!=0){
                                                chooseShow.clear();
                                                for (String name: choose) {
                                                    chooseShow.add(name);
                                                }
                                            }
                                            if(nameid.size()!=0){
                                                nameid2.clear();
                                                for (String name: nameid) {
                                                    nameid2.add(name);
                                                }
                                            }
                                            TextView t = view.findViewById(R.id.listview_text_data);
                                            t.setTextColor(Color.BLACK);
                                            String names=null;
                                            int i=0;
                                            for (String name: chooseShow) {
                                                if(i==0){
                                                    if(chooseShow.size()==1){
                                                        names=name;
                                                    }else{
                                                        names=name+",";
                                                    }
                                                }else if(i!=chooseShow.size()-1){
                                                    names=names+name+",";
                                                }else{
                                                    names=names+name;
                                                }
                                                i++;
                                            }
                                            t.setText(names);
                                        }
                                    })
                                    .create();
                            builder.show();
                            builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(13);
                            // 通过Activity类中的getWindowManager()方法获取窗口管理，再调用getDefaultDisplay()方法获取获取Display对象
                            Display display = getActivity().getWindowManager().getDefaultDisplay();
                            // 方法一(推荐使用)使用Point来保存屏幕宽、高两个数据
                            Point outSize = new Point();
                            // 通过Display对象获取屏幕宽、高数据并保存到Point对象中
                            display.getSize(outSize);
                            int x = (new Double(outSize.x*0.9)).intValue();
                            int y = (new Double(outSize.y*0.9)).intValue();

                            Drawable dr = getActivity().getResources().getDrawable(R.drawable.login_btn_selector); //取得图片资源
                            builder.getButton(AlertDialog.BUTTON_POSITIVE).setBackground(dr);
                            LinearLayout.LayoutParams PositiveButton =
                                    (LinearLayout.LayoutParams) builder.getButton(DialogInterface.BUTTON_POSITIVE).getLayoutParams();
                            PositiveButton.height = (new Double(outSize.x*0.12)).intValue();
                            PositiveButton.width = (new Double(outSize.x*0.4)).intValue();
                            //设置文字居中
                            PositiveButton.gravity = Gravity.CENTER;
                            PositiveButton.setMargins((x-(new Double(outSize.x*0.4)).intValue())/2,
                                    20, (x-(new Double(outSize.x*0.4)).intValue())/2, 20);

                        }

                    }break;


                }
            }
        });
        //}
        //  else if(bianjiid==2){
//
        //      getOperatingByPkId ger = new getOperatingByPkId();
        //      for(getOperatingByPkId g :lista){
        //          ger=g;
        //      }
//
//
        //      ListviewBean<String> list1 = new ListviewBean();
        //      list1.setData(ger.getOperatingHouseholds());
        //      list1.setImageSrc(R.mipmap.operator_name);
        //      list1.setName("姓名");
//

        //      ListviewBean<String> list2 = new ListviewBean();
        //      list2.setData(ger.getBoothNumber());
        //      list2.setImageSrc(R.mipmap.operator_booth_number);
        //      list2.setName("摊位号");


        //      ListviewBean<String> list3 = new ListviewBean();
        //      list3.setData(ger.getName());
        //      list3.setImageSrc(R.mipmap.sample_category);
        //      list3.setName("市场名称");
        //      list3.setRedStar(true);

        //      ListviewBean<String> list4 = new ListviewBean();
        //      list4.setData(ger.getPhone());
        //      list4.setImageSrc(R.mipmap.listview_sample_name);
        //      list4.setName("联系电话");


        //      ListviewBean<String> list5 = new ListviewBean();
        //      list5.setData(ger.getName1());
        //      list5.setImageSrc(R.mipmap.listview_text_test_type);
        //      list5.setName("经营种类");
        //      list5.setRedStar(true);

        //      ListviewBean<String> list6 = new ListviewBean();
        //      list6.setData(ger.getBusiness_license());
        //      list6.setImageSrc(R.mipmap.listview_operator_social_number);
        //      list6.setName("统一社会信用代码");


//        ListviewBean<Bitmap> list6=new ListviewBean();
//        Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
//        list6.setData(bitmap);
//        list6.setImageSrc(R.mipmap.listview_text_test_method);
//        list6.setName("采样照片");
//        list6.setComplicated(true);





        //    List<ListviewBean> list = new ArrayList<>();
//
        //    list.add(list1);
        //    list.add(list2);
        //    list.add(list4);
        //    List<ListviewBean> listE = new ArrayList<>();
        //    listE.add(list3);
        //    listE.add(list5);
        //    List<ListviewBean> listXinyong = new ArrayList<>();
        //    listXinyong.add(list6);
//
        //    listviewEditTextAdapter=new ListviewEditTextAdapter(list, getContext());
        //    sampleTextListview.setAdapter(listviewEditTextAdapter);
//
        //    sampleTextListviewXiala.setAdapter(new ListviewTextAdapter(listE,getContext(),false));
        //    listviewEditTextAdapter1=new ListviewEditTextAdapter(listXinyong, getContext());
        //    sampleTextListviewXinyong.setAdapter(listviewEditTextAdapter1);
        //    sampleTextListviewXiala.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        //      @Override
        //      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //          switch (position)
        //          {
        //              case 0:{


        //                  if (marketList.size()!=0) {

        //                      String s[]=new String[marketList.size()];
        //                      for (int i=0;i<marketList.size();i++)
        //                      {
        //                          s[i]=marketList.get(i).getName();
        //                      }


        //                      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        //                      builder.setTitle("请选择市场");
        //                      builder.setIcon(R.mipmap.ic_launcher);
        //                     builder.setItems(s, new DialogInterface.OnClickListener() {
        //                         @Override
        //                         public void onClick(DialogInterface dialog, int which) {
        //                             dialog.dismiss();
        //                             TextView t = view.findViewById(R.id.listview_text_data);
        //                             t.setText(marketList.get(which).getName());
        //                             if (marketList.get(which).getPkId()!=null){
        //                                 //System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getName());
        //                                 //System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getPkId());
        //                                 operatingBean.setMarketId(marketList.get(which).getPkId());
        //                             }
        //                         }
        //                     });
        //                     builder.create().show();
        //                 }
        //             }break;
        //             case 1:{
        //                 if (typeList!=null&&typeList.size()!=0) {

        //                    String s[]=new String[typeList.size()];
        //                    for (int i=0;i<typeList.size();i++)
        //                    {
        //                        s[i]=typeList.get(i).getName();
        //                    }
        //                    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        //                    builder.setTitle("请选择经营种类");
        //                    builder.setIcon(R.mipmap.ic_launcher);
        //                    builder.setItems(s, new DialogInterface.OnClickListener() {
        //                        @Override
        //                        public void onClick(DialogInterface dialog, int which) {
        //                            dialog.dismiss();
        //                            TextView t = view.findViewById(R.id.listview_text_data);
        //                            t.setText(typeList.get(which).getName());
        //                            if (typeList.get(which).getPkId()!=null){
        //                                // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getName());
        //                                // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+marketList.get(which).getPkId());
        //                                operatingBean.setTypePkId(typeList.get(which).getPkId());
        //                            }
        //                        }
        //                    });
        //                    builder.create().show();
        //                }
        //            }break;
        //        }
        //    }
        //});
        // }
        // textlistView.setAdapter(new ListviewTextAdapter(list,getContext()));
//        imagelistView.setAdapter(new ListviewRichTextAdapter(list,getContext()));

        initUI();
        addPlus();
    }
    @OnClick(R.id.submit)
    public void OnClick(){
//        InsertOperatingRequest insertOperatingRequest=new InsertOperatingRequest();
//        sampleTextListview.getAdapter().getView()
//
//        for (int i=0;i<sampleTextListview.getAdapter().getCount();i++)
//        {
//            View view =sampleTextListview.getAdapter().getView(i,getView(),null);
//            view.findViewById(R.id.)
//
//
//        }
//            insertOperatingRequest.setAccountPkId();
        InsertOperatingRequest insertOperatingRequest =new InsertOperatingRequest();
        insertOperatingRequest.setAccountPkId(BaseApplication.getAccount().getPkId());
        // operatingBean.set( listviewEditTextAdapter.list.get(1).getData().toString());
        //operatingBean.setAccountPkId(BaseApplication.getAccount().getPkId());
        //if(bianjiid==1){
        if(!TextUtils.isEmpty(listviewEditTextAdapter.list.get(0).getData().toString())&&!"请选择".equals(listviewEditTextAdapter.list.get(0).getData().toString())){
            if(!TextUtils.isEmpty(listviewEditTextAdapter.list.get(1).getData().toString())&&!"请选择".equals(listviewEditTextAdapter.list.get(1).getData().toString())){
                if(!TextUtils.isEmpty(sampleTextListviewXiala.toString())){
                    if(nameid2.size()!=0){
                        operatingBean.setBusinesslicense(listviewEditTextAdapter1.list.get(0).getData().toString());
                        operatingBean.setOperatingHouseholds(listviewEditTextAdapter.list.get(0).getData().toString());
                        operatingBean.setBoothNumber(listviewEditTextAdapter.list.get(1).getData().toString());
                        operatingBean.setPhone(listviewEditTextAdapter.list.get(2).getData().toString());
                        if(gridAdapter.getImageSize()!=0) {
                            Bitmap b = PhotoBitmapUtil.getCompressPhoto((String) gridAdapter.getItem(0));
                            operatingBean.setPhoto(ImageUtil.toBase64String(b));
                            System.out.println("图片的base" + ImageUtil.toBase64String(b));
                        }else{
                            operatingBean.setPhoto("");
                        }
                        insertOperatingRequest.setOperating(operatingBean);
                        insertOperatingRequest.setTypeList(nameid2);
                        addOperatorPresenter.getAddOperatorPresenterResponseInfo(insertOperatingRequest);
                    }else{
                        Toast.makeText(getContext(),"经营种类不能为空!!!!",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(getContext(),"市场名称不能为空!!!!",Toast.LENGTH_LONG).show();
                }
            }else{
                Toast.makeText(getContext(),"摊位号不能为空!!!!",Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(getContext(),"姓名不能为空!!!!",Toast.LENGTH_LONG).show();
        }
        //  }else if(bianjiid==2){
        //      if(listviewEditTextAdapter.list.get(0).getData().toString()!=null){

        //          if(listviewEditTextAdapter.list.get(1).getData().toString()!=null){

        //              operatingBean.setBusinesslicense(listviewEditTextAdapter1.list.get(0).getData().toString());
        //              operatingBean.setOperatingHouseholds(listviewEditTextAdapter.list.get(0).getData().toString());
        //              operatingBean.setBoothNumber(listviewEditTextAdapter.list.get(1).getData().toString());
        //              operatingBean.setPhone(listviewEditTextAdapter.list.get(2).getData().toString());
        //              // Bitmap b = PhotoBitmapUtil.getCompressPhoto((String) gridAdapter.getItem(0));
        //              // operatingBean.setPhoto(ImageUtil.toBase64String(b));

        //              insertOperatingRequest.setOperating(operatingBean);
        //              addOperatorPresenter.getAddOperatorPresenterResponseInfo(insertOperatingRequest);
        //          }else{
        //              Toast.makeText(getContext(),"摊位号不能为空!!!!",Toast.LENGTH_LONG).show();
        //          }
        //      }else{
        //          Toast.makeText(getContext(),"姓名不能为空!!!!",Toast.LENGTH_LONG).show();
        //      }
        //}


    }

    @OnClick(R.id.fanhui)
    public void fanhui(){

        Intent intent=new Intent(context, BusinessArchivesActivity.class);
        context.startActivity(intent);
    }
    private void initUI() {
        mGridView = (CustomScrollGridView) getActivity().findViewById(R.id.gv_all_photo);

    }
    private void addPlus() {
        String kongimage=null;
        gridAdapter = new PictureAddGridAdapter(view.getContext(), 1,kongimage);
        mGridView.setAdapter(gridAdapter);

        //   getActivity().getSupportFragmentManager().saveFragmentInstanceState(SampleFragment.newInstance());
        // 选择图片获取途径
        selectPhoto = new PhotoSystemOrShoot(view.getContext()) {
            @Override
            public void onStartActivityForResult(Intent intent, int requestCode) {

                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//                    Uri contentUri = FileProvider.getUriForFile(context,"com.skyrin.bingo.fileprovider",apkFile);
//                    intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
                    startActivityForResult(intent, requestCode);

                }else {
                    //        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent.setDataAndType(
//                            Uri.fromFile(apkFile),
//                            "application/vnd.android.package-archive");
                    startActivityForResult(intent, requestCode);

                }

            }
        };
        commandPhoto = new CommandPhotoUtil(getContext(), mGridView, gridAdapter, selectPhoto);
    }



    @Override
    public void onError(String result) {
        Toast.makeText(getContext(),result,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(Object resultData) {
        if (resultData!=null)
        {
            //       Log.d("qqqqqq","onSuccess");
            //         CallbackData<List<Market>> callbackData=(CallbackData<List<Market>>)resultData;

        }
    }

    @Override
    public void insertOperator(Callback callback) {
        if (callback.getState()==1)
        {
            //跳转activity
            Intent intent = new Intent(getContext(), BusinessArchivesActivity.class);
            startActivity(intent);
            //          getFragmentManager().on();//suport.v4包
        }else if(callback.getState()==200){
            Toast.makeText(getContext(),callback.getAlertMessage(),Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void getOperatorLists(Object resultData) {
        CallbackData callbackData=(CallbackData<List<Market>>)resultData;
        marketList=(List<Market>)callbackData.getData();
    }

    @Override
    public void getOperatorListtype(Object resultData) {
//        CallbackData callbackData=(CallbackData<List<getOperatingType>>)resultData;
//        typeList=(List<getOperatingType>)callbackData.getData();
    }

    @Override
    public void getSamplingName(Object resultData) {
        typelists=(CallbackData<List<TypeList>>)resultData;
        //typeList=(List<TypeList>)callbackData.getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        samplePresenter.BindPresentView(this);
        samplePresenter.onCreate();
        initRequest();

    }

    public  void initRequest(){
        SimpleRequest s=new SimpleRequest();
        s.setAccountPkId(BaseApplication.getAccount().getPkId());
        samplePresenter.getAddOperatorPresenterResponseInfo(s);
        samplePresenter.getSamplingNameResponseInfo(s);


    }
}
